package com.ruoyi.receipt.service.impl;

import com.ruoyi.common.exception.CustomException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.IdWorker;
import com.ruoyi.common.utils.MapCheckUtil;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.receipt.domain.TbCaUser;
import com.ruoyi.receipt.domain.TbUser;
import com.ruoyi.receipt.mapper.TbUserMapper;
import com.ruoyi.receipt.service.ITbCaUserService;
import com.ruoyi.receipt.service.ITbUserRelationService;
import com.ruoyi.receipt.service.ITbUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestBody;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 用户信息Service业务层处理
 * 
 * @author ruoyi
 * @date 2021-03-15
 */
@Service
public class TbUserServiceImpl implements ITbUserService 
{
    @Autowired
    private TbUserMapper tbUserMapper;

    @Autowired
    private ITbCaUserService tbCaUserService;

    @Autowired
    private IdWorker idWorker;

    @Autowired
    private ITbUserRelationService tbUserRelationService;

    /**
     * 查询用户信息
     * 
     * @param id 用户信息ID
     * @return 用户信息
     */
    @Override
    public TbUser selectTbUserById(Long id)
    {
        return tbUserMapper.selectTbUserById(id);
    }

    /**
     * 查询用户信息列表
     * 
     * @param tbUser 用户信息
     * @return 用户信息
     */
    @Override
    public List<TbUser> selectTbUserList(TbUser tbUser)
    {
        return tbUserMapper.selectTbUserList(tbUser);
    }

    /**
     * 新增用户信息
     * 
     * @param tbUser 用户信息
     * @return 结果
     */
    @Override
    public int insertTbUser(TbUser tbUser)
    {
        tbUser.setCreateTime(DateUtils.getNowDate());
        return tbUserMapper.insertTbUser(tbUser);
    }

    /**
     * 修改用户信息
     * 
     * @param tbUser 用户信息
     * @return 结果
     */
    @Override
    public int updateTbUser(TbUser tbUser)
    {
        tbUser.setUpdateTime(DateUtils.getNowDate());
        return tbUserMapper.updateTbUser(tbUser);
    }

    /**
     * 批量删除用户信息
     * 
     * @param ids 需要删除的用户信息ID
     * @return 结果
     */
    @Override
    public int deleteTbUserByIds(Long[] ids)
    {
        return tbUserMapper.deleteTbUserByIds(ids);
    }

    /**
     * 删除用户信息信息
     * 
     * @param id 用户信息ID
     * @return 结果
     */
    @Override
    public int deleteTbUserById(Long id)
    {
        return tbUserMapper.deleteTbUserById(id);
    }


    /**
     * 注册用户
     *
     * @param map 用户信息map
     */
    @Override
    @Transactional
    public TbUser registNewUser(Map map){
        Long newUserId=idWorker.nextId();
        MapCheckUtil.requiredCheck(map,new String[]{"loginAccount","type","deviceId","pwd","ca1","ca1answer","ca2","ca2answer","ca3","ca3answer"});

        //校验两次密码是否一直
        String qPwd= map.get("qPwd")+"";
        String pwd=  map.get("pwd")+"";
        if(!qPwd.equals(pwd)){
            throw new CustomException("两次密码不一致");
        }

        //设置用户基本信息
        TbUser newUser = new TbUser();
        newUser.setId(newUserId);
        newUser.setLoginAccount(map.get("loginAccount")+"");
        newUser.setIsDel(0);
        newUser.setType(Integer.parseInt(map.get("type")+""));
        newUser.setDeviceId((map.get("deviceId")+""));
        newUser.setPwd(SecurityUtils.encryptPassword(map.get("pwd")+""));
        insertTbUser(newUser);

        //设置密保
        setCaByUserId(newUserId,map);

        return newUser;

    }


    /**
     * 设置密保
     * @param userId 用户id
     * @param map 用户密保信息
     */
    @Override
    public void setCaByUserId(Long userId,Map map){

        //密保
        TbCaUser tbCaUser1=new TbCaUser(idWorker.nextId(),userId,Long.parseLong(map.get("ca1")+""),map.get("ca1answer")+"",0);
        tbCaUserService.insertTbCaUser(tbCaUser1);

        TbCaUser tbCaUser2=new TbCaUser(idWorker.nextId(),userId,Long.parseLong(map.get("ca2")+""),map.get("ca2answer")+"",0);
        tbCaUserService.insertTbCaUser(tbCaUser2);

        TbCaUser tbCaUser3=new TbCaUser(idWorker.nextId(),userId,Long.parseLong(map.get("ca3")+""),map.get("ca3answer")+"",0);
        tbCaUserService.insertTbCaUser(tbCaUser3);

    }

    /**
     * 重置密码和密保
     * @param userId 用户id
     * @param map 用户密保信息
     */
    @Override
    @Transactional
    public void resetCaAndPwd(Long userId,Map map){

        //设置密码
        TbUser tbUser=new TbUser();
        tbUser.setId(userId);
        tbUser.setIsReset(0);
        tbUser.setPwd(SecurityUtils.encryptPassword(map.get("pwd")+""));
        updateTbUser(tbUser);

        //设置密保
        setCaByUserId(userId,map);
        //密保
        TbCaUser tbCaUser1=new TbCaUser(idWorker.nextId(),userId,Long.parseLong(map.get("ca1")+""),map.get("ca1answer")+"",0);
        tbCaUserService.insertTbCaUser(tbCaUser1);

        TbCaUser tbCaUser2=new TbCaUser(idWorker.nextId(),userId,Long.parseLong(map.get("ca2")+""),map.get("ca2answer")+"",0);
        tbCaUserService.insertTbCaUser(tbCaUser2);

        TbCaUser tbCaUser3=new TbCaUser(idWorker.nextId(),userId,Long.parseLong(map.get("ca3")+""),map.get("ca3answer")+"",0);
        tbCaUserService.insertTbCaUser(tbCaUser3);

    }

    /**
     * 根据登录账号和角色
     * 1.查询登录账号的角色个数  typeNumber
     * 2.查询当前登录账号+角色是否存在 true为存在 false为不存在
     */
    @Override
    public Map findLoginAccountInfo(@RequestBody String loginAccount, String type) {

        TbUser tempUser=new TbUser();
        tempUser.setLoginAccount(loginAccount);
        tempUser.setIsDel(0);

        TbUser resultUser=new TbUser();

        Map resultMap=new HashMap();
        List<TbUser> tbUsers = selectTbUserList(tempUser);
        boolean isExist=false;
        for (int i = 0; i < tbUsers.size(); i++) {
            TbUser tbUser=tbUsers.get(i);
            if(tbUser.getLoginAccount().equals(loginAccount)&&(tbUser.getType()+"").equals(type)){
                isExist=true;
                resultUser=tbUser;
                break;
            }
        }
        resultMap.put("typeNumber",tbUsers.size());
        resultMap.put("tbUser",resultUser);
        resultMap.put("isExist",isExist);
        return resultMap;
    }

    /**
     * 修改设备编号
     *
     * @param userId 用户id
     * @param deviceId 设备id
     */
    @Override
    public void updateDeviceId(Long userId,String deviceId){
        //登陆成功后,修改deviceId
        TbUser updateDeviceIdUser=new TbUser();
        updateDeviceIdUser.setId(userId);
        updateDeviceIdUser.setDeviceId(deviceId);
        updateDeviceIdUser.setLastLoginTime(new Date());
        updateTbUser(updateDeviceIdUser);
    }
}
